Skip to content

perf: avoid JSON/map roundtrips in ast.Value transforms#1606

Merged
anderseknert merged 1 commit intomainfrom
new
Jun 24, 2025
Merged

perf: avoid JSON/map roundtrips in ast.Value transforms#1606
anderseknert merged 1 commit intomainfrom
new

Conversation

@anderseknert
Copy link
Copy Markdown
Member

Leveraging the new capabilities for this in roast, we can avoid a lot of allocations by mapping directly from a source to an ast.Value rather than going via a map[string]any, or even JSON. For now, focus has been on how the input is mapped in most places of eval. This can be improved further, but the natural next step is of course to eliminate roundtrips the same way when we handle output, i.e. the result of evaluation. I imagine the data-heavy aggregate rules in particular could benefit greatly from this.

This PR also cleans up some in LSP handlers related to input transformations, and takes a small step towards harmonizing the input format for those with the goal of eventually having a single schema to describe the common parts of all LSP request payloads.

Perf impact is close to 2.9 million allocs less in the Regal lint benchmark, and greatly reduced ns/op and B/op.

944216438 ns/op    2805943268 B/op 51659258 allocs/op - main
894474146 ns/op    2668457832 B/op 48779590 allocs/op - PR

Comment thread pkg/version/version.go Outdated
Leveraging the new capabilities for this in roast, we can avoid
a lot of allocations by mapping directly from a source to an ast.Value
rather than going via a `map[string]any`, or even JSON. For now, focus
has been on how the **input** is mapped in most places of eval.
This can be improved further, but the natural next step is of course to
eliminate roundtrips the same way when we handle **output**, i.e. the
result of evaluation. I imagine the data-heavy aggregate rules in particular
could benefit greatly from this.

This PR also cleans up some in LSP handlers related to input transformations,
and takes a small step towards harmonizing the input format for those with
the goal of eventually having a single schema to describe the common parts
of all LSP request payloads.

Perf impact is close to 2.9 million allocs less in the Regal lint
benchmark, and greatly reduced ns/op and B/op.

```
944216438 ns/op    2805943268 B/op 51659258 allocs/op - main
894474146 ns/op    2668457832 B/op 48779590 allocs/op - PR
```

Signed-off-by: Anders Eknert <anders@styra.com>
Copy link
Copy Markdown
Contributor

@charlieegan3 charlieegan3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Great work!

@anderseknert anderseknert merged commit 112dc87 into main Jun 24, 2025
6 checks passed
@anderseknert anderseknert deleted the new branch June 24, 2025 11:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants